using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;

namespace uTorrentNetClient.ModelDesign.Converter
{
    [ValueConversion(typeof(TimeSpan), typeof(string))]
    public class TimeSpanDisplayConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(string))
            {
                return null;
            }
            if (value is TimeSpan)
            {
                TimeSpan TimeSpanToConvert = (TimeSpan)value;
                if (TimeSpanToConvert == TimeSpan.MaxValue)
                {
                    return "∞";
                }
                if (TimeSpanToConvert.TotalDays > 365)
                {
                    return (String.Format("{0}y {1}w", TimeSpanToConvert.Days / 365, (TimeSpanToConvert.Days % 365) / 7));
                }
                if (TimeSpanToConvert.TotalDays >= 15)
                {
                    return (String.Format("{0}w {1}d", TimeSpanToConvert.Days / 7, TimeSpanToConvert.Days % 7));
                }
                if (TimeSpanToConvert.TotalHours >= 24)
                {
                    return (String.Format("{0}d {1}h", TimeSpanToConvert.Days, TimeSpanToConvert.Hours));
                }
                if (TimeSpanToConvert.TotalHours >= 1)
                {
                    return (String.Format("{0}h {1}m", TimeSpanToConvert.Hours, TimeSpanToConvert.Minutes));
                }
                if (TimeSpanToConvert.TotalMinutes >= 1)
                {
                    return (String.Format("{0}m {1}s", TimeSpanToConvert.Minutes, TimeSpanToConvert.Seconds));
                }
                return (String.Format("{0}s", TimeSpanToConvert.Seconds));
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
